//
//  MoreViewController.m
//  WXWeibo
//
//  Created by apple on 16/5/9.
//  Copyright © 2016年 zic_magic. All rights reserved.
//

#import "MoreViewController.h"
#import "ThemeManager.h"
#import "ThemeLabel.h"
#import "ThemeImageView.h"
#import "ThemeSelectViewController.h"

@interface MoreViewController ()

@property (weak, nonatomic) IBOutlet  ThemeImageView *themeSelectImg;
@property (weak, nonatomic) IBOutlet ThemeLabel *themeSelectLabel;
@property (weak, nonatomic) IBOutlet ThemeLabel *themeLabel;
@property (weak, nonatomic) IBOutlet ThemeImageView *userManagerImg;
@property (weak, nonatomic) IBOutlet ThemeLabel *userManagerLabel;
@property (weak, nonatomic) IBOutlet ThemeImageView *suggestionImg;
@property (weak, nonatomic) IBOutlet ThemeLabel *suggestionLabel;
@property (weak, nonatomic) IBOutlet ThemeImageView *cleanImg;
@property (weak, nonatomic) IBOutlet ThemeLabel *cleanLabel;
@property (weak, nonatomic) IBOutlet ThemeLabel *sizeLabel;
@property (weak, nonatomic) IBOutlet ThemeLabel *exitLabel;


@property (weak, nonatomic) IBOutlet UITableViewCell *cell1;
@property (weak, nonatomic) IBOutlet UITableViewCell *cell2;
@property (weak, nonatomic) IBOutlet UITableViewCell *cell3;
@property (weak, nonatomic) IBOutlet UITableViewCell *cell4;
@property (weak, nonatomic) IBOutlet UITableViewCell *cell5;

@end

@implementation MoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"More";
    
    
    [self themeChangeAction];
    
}



- (void)themeChangeAction {
    
    _themeSelectImg.imgName = @"more_icon_theme";
    _themeSelectLabel.colorName = @"More_Item_Text_color";
    
    _userManagerImg.imgName = @"more_icon_account";
    _userManagerLabel.colorName = @"More_Item_Text_color";
    
    _suggestionImg.imgName = @"more_icon_feedback";
    _suggestionLabel.colorName = @"More_Item_Text_color";

    _cleanImg.imgName = @"more_icon_draft";
    _cleanLabel.colorName = @"More_Item_Text_color";
    
    _themeLabel.colorName = @"More_Item_Text_color";
    _sizeLabel.colorName = @"More_Item_Text_color";
    _exitLabel.colorName = @"More_Item_Text_color";
    

    
    ThemeManager *manager = [ThemeManager shareManager];
    self.tableView.backgroundColor = [manager themeColorWithColorName:@"More_Item_color"];
    _cell1.backgroundColor = [manager themeColorWithColorName:@"More_Item_color"];
    _cell2.backgroundColor = [manager themeColorWithColorName:@"More_Item_color"];
    _cell3.backgroundColor = [manager themeColorWithColorName:@"More_Item_color"];
    _cell4.backgroundColor = [manager themeColorWithColorName:@"More_Item_color"];
    _cell5.backgroundColor = [manager themeColorWithColorName:@"More_Item_color"];
    
    [self.tableView setSeparatorColor:[manager themeColorWithColorName:@"More_Item_Line_color"]];

    
    _themeLabel.text = manager.themeName;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 1 && indexPath.section == 1) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"确定清除缓存%@", self.sizeLabel.text] preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
            [self clearCatch];
            [self readCatchSize];
            
            
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:NULL]];
        [self presentViewController:alert animated:YES completion:NULL];
        
    }
    if (indexPath.row == 0 && indexPath.section == 0) {
        
        ThemeSelectViewController *tsVC =  [[ThemeSelectViewController alloc] init];
        
        tsVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:tsVC animated:YES];
        
    }
    
}



- (NSInteger)getCatchSize {
    
    NSInteger fileSize = 0;
    
    NSString *catchPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
    NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:catchPath];
    
    for (NSString *fileName in fileEnumerator) {
        
        NSString *filePath = [catchPath stringByAppendingPathComponent:fileName];
        NSDictionary *attDic = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
        fileSize += [attDic fileSize];
        
    }
    return fileSize;
}




- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:YES];
    
    [self readCatchSize];
    [self.tableView reloadData];
    [self themeChangeAction];
}


- (void)readCatchSize {
    
    NSInteger fileSize = [self getCatchSize];
    self.sizeLabel.text = [NSString stringWithFormat:@"%.2f MB", fileSize / 1024.0 / 1024.0];
    
}

- (void)clearCatch {
    
    
    NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
    [[NSFileManager defaultManager] removeItemAtPath:filePath error:NULL];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
